<!--
作者：T先森
QQ：1379146389
官网：doic.cn
本免费开源使用，请遵循开源协议，勿用于商业转售、闭源改造。二次开发需保留版权声明与开源标识，衍生代码同样开源。有疑问可联系作者反馈。 
-->

<?php
session_start();
// 指定图片存储的文件夹
$imageDir = 'p/';

// 获取文件夹中的所有文件，并过滤出图片和视频文件
$imageFiles = [];
$allowedTypes = ['jpg', 'jpeg', 'png', 'gif', 'mp4', 'ico'];
foreach (scandir($imageDir) as $file) {
    $filePath = $imageDir. $file;
    if (is_file($filePath) && in_array(strtolower(pathinfo($filePath, PATHINFO_EXTENSION)), $allowedTypes)) {
        $imageFiles[$file] = filemtime($filePath); // 使用文件名作为键，文件修改时间作为值
    }
}

// 按照文件修改时间进行排序，最新的在前
arsort($imageFiles);

// 格式化文件大小函数
function formatBytes($bytes, $precision = 2) {
    $units = array('B', 'KB', 'MB', 'GB', 'TB');

    $bytes = max($bytes, 0);
    $pow = floor(($bytes? log($bytes) : 0) / log(1024));
    $pow = min($pow, count($units) - 1);

    $bytes /= (1 << (10 * $pow));

    return round($bytes, $precision). ($pow > 1? 'MB' :''. $units[$pow]);
}

// 设置页面访问密码
define('ACCESS_PASSWORD', '6');

// 检查页面访问密码
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['access_password'])) {
    $accessPassword = $_POST['access_password'];
    if ($accessPassword === ACCESS_PASSWORD) {
        $_SESSION['authenticated'] = true;
    } else {
        echo '404页面无法访问！';
        exit;
    }
} elseif (!isset($_SESSION['authenticated'])) {
    // 显示密码输入页面
    ?>
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>爱看 - 图床云</title>
        <link rel="icon" href="https://doic.cn/d/p/108.png" type="image/x-icon">
        <meta name="viewport" content="width=device-width, initial-scale=0.74, maximum-scale=1, user-scalable=no">
        <style>
            body {
                font-family: 'Montserrat', sans-serif;
                background-color: #e6f7ff; /* 新的背景色，浅蓝色 */
                color: #333; /* 主要文字颜色为深色 */
                margin: 0;
                padding: 20px;
                display: flex;
                flex-direction: column;
                align-items: center;
            }
            h1 {
                color: #007acc; /* 标题颜色为深蓝色 */
                text-align: center;
                font-size: 2.5em;
                margin-bottom: 30px;
                letter-spacing: 2px;
            }
            form {
                display: flex;
                flex-direction: column;
                align-items: center;
            }
            input {
                padding: 10px;
                margin-bottom: 10px;
                border: 1px solid #ccc;
                border-radius: 5px;
            }
            button {
                padding: 10px 20px;
                background-color: #007acc;
                color: white;
                border: none;
                border-radius: 5px;
                cursor: pointer;
            }
        </style>
    </head>
    <body>
        <h1>AiKan TyunOS</h1>
        <form method="post">
            <input type="password" name="access_password" placeholder="输入访问密码">
            <button type="submit">提交</button>
        </form>
    </body>
    </html>
    <?php
    exit;
}

if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['filename']) && isset($_POST['confirm_delete'])) {
    $filename = $_POST['filename'];
    $confirmDelete = $_POST['confirm_delete'];

    if ($confirmDelete!== 'yes') {
        echo '删除未确认，操作取消。';
        exit;
    }

    $filePath = 'p/'. basename($filename);

    if (is_file($filePath)) {
        if (unlink($filePath)) {
            echo '文件删除成功。';
        } else {
            echo '删除文件时出错。';
        }
    } else {
        echo '文件不存在。';
    }
} else {
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>爱看 - 图床云</title>
    <link rel="icon" href="https://doic.cn/d/p/108.png" type="image/x-icon">
    <meta name="viewport" content="width=device-width, initial-scale=0.74, maximum-scale=1, user-scalable=no">
    <style>
        body {
            font-family: 'Montserrat', sans-serif;
            background-color: #e6f7ff; /* 新的背景色，浅蓝色 */
            color: #333; /* 主要文字颜色为深色 */
            margin: 0;
            padding: 20px;
            display: flex;
            flex-direction: column;
            align-items: center;
        }
        h1 {
            color: #007acc; /* 标题颜色为深蓝色 */
            text-align: center;
            font-size: 2.5em;
            margin-bottom: 30px;
            letter-spacing: 2px;
        }
        p {
            font-size: 1.2em;
            color: #666; /* 段落文字颜色为灰色 */
            max-width: 800px;
            text-align: center;
            line-height: 1.8;
        }
      .gallery {
            display: flex;
            flex-wrap: wrap;
        }
      .gallery img,.gallery video {
            width: 120px; /* 图片和视频缩略图宽度 */
            height: 120px;
            margin: 10px;
            object-fit: cover; /* 保持图片和视频缩略图的比例，裁剪溢出部分 */
            transition: transform 0.2s ease-in-out;
            cursor: pointer;
        }
      .gallery img.enlarged,.gallery video.enlarged {
            transform: scale(4); /* 放大比例增加 */
        }
      .image-info {
            text-align: center;
            font-size: 10px;
            margin-top: -8px;
        }
      .overlay {
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background: rgba(0, 0, 0, 0.8);
            display: none;
            justify-content: center;
            align-items: center;
            z-index: 1000;
        }
      .overlay img,.overlay video {
            max-width: 90%;
            max-height: 90%;
            transition: transform 0.2s ease-in-out;
        }
        
    </style>
</head>
<body>
    <h1>AiKan TyunOS</h1>
    <br>
    <div class="gallery">
        <?php foreach ($imageFiles as $file => $modifiedTime):?>
            <?php
            $filePath = $imageDir. $file;
            $fileInfo = pathinfo($filePath);
            $modifiedTime = date("y-n-j G:i", $modifiedTime);
            $fileSize = filesize($filePath); // 获取文件大小
            $fileSizeFormatted = formatBytes($fileSize); // 格式化文件大小
            $fileExt = strtolower($fileInfo['extension']);
          ?>
            <div>
                <?php if (in_array($fileExt, ['jpg', 'jpeg', 'png', 'gif', 'ico'])):?>
                    <img src="<?= $filePath?>" alt="<?= $file?>" onclick="enlargeMedia(this)">
                <?php elseif ($fileExt == 'mp4'):?>
                    <video src="<?= $filePath?>" onclick="enlargeMedia(this)"></video>
                <?php endif;?>
                <div class="image-info">
                    <span class="random-color"><b>ID：<?= $fileInfo['filename']?></b></span><br>
                    上传日期: <?= $modifiedTime?><br>
                    大小: <?= $fileSizeFormatted?>
                    <!-- 修改复制链接部分 -->
                    <span style="color: green; cursor: pointer;" onclick="copyToClipboard('<?= (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on'? 'https' : 'http'). '://'. $_SERVER['HTTP_HOST']. '/d/p/'. $fileInfo['basename']?>')">复制</span>
                    <!-- 删除文件 -->
                    <span style="color: red; cursor: pointer;" onclick="confirmDelete('<?= $fileInfo['basename']?>')">删除</span>
                </div>
            </div>
        <?php endforeach;?>
    </div>

    <div class="overlay" onclick="shrinkMedia()">
        <img id="overlay-img" src="" style="display:none;">
        <video id="overlay-video" controls style="display:none;"></video>
    </div>

    <script>
    function confirmDelete(fileName) {
        var confirmDelete = confirm("你确定要删除这个文件吗？");
        if (confirmDelete) {
            deleteFile(fileName);
        }
    }

    function deleteFile(fileName) {
        var xhr = new XMLHttpRequest();
        xhr.open('POST', '', true);
        xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
        xhr.onreadystatechange = function() {
            if (xhr.readyState == 4 && xhr.status == 200) {
                alert(xhr.responseText);
                location.reload();
            }
        };
        xhr.send('filename=' + fileName + '&confirm_delete=yes');
    }

    function enlargeMedia(media) {
        var overlay = document.querySelector('.overlay');
        var overlayImg = document.getElementById('overlay-img');
        var overlayVideo = document.getElementById('overlay-video');

        if (media.tagName.toLowerCase() === 'img') {
            overlayImg.src = media.src;
            overlayImg.style.display = 'block';
            overlayVideo.style.display = 'none';
        } else if (media.tagName.toLowerCase() === 'video') {
            overlayVideo.src = media.src;
            overlayVideo.style.display = 'block';
            overlayImg.style.display = 'none';
        }

        overlay.style.display = 'flex';
    }

    function shrinkMedia() {
        var overlay = document.querySelector('.overlay');
        var overlayImg = document.getElementById('overlay-img');
        var overlayVideo = document.getElementById('overlay-video');

        overlayImg.style.display = 'none';
        overlayVideo.style.display = 'none';
        overlay.style.display = 'none';
    }

    function copyToClipboard(text) {
        var input = document.createElement('input');
        input.setAttribute('value', text);
        document.body.appendChild(input);
        input.select();
        document.execCommand('copy');
        document.body.removeChild(input);
        alert('已复制图片链接');
    }
    </script>
</body>

<!--统计代码请勿删除--><script>
var _hmt = _hmt || [];
(function() {
  var hm = document.createElement("script");
  hm.src = "https://hm.baidu.com/hm.js?82d3850e9cffef7bdfde3db3e6e4b978";
  var s = document.getElementsByTagName("script")[0]; 
  s.parentNode.insertBefore(hm, s);
})();
</script>
<script charset="UTF-8" id="LA_COLLECT" src="//sdk.51.la/js-sdk-pro.min.js"></script>
<script>LA.init({id:"KFunBsxDl2bThYnS",ck:"KFunBsxDl2bThYnS"})</script>
</html>
<?php }?>